home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / STRNREV.C < prev    next >
Text File  |  1993-01-04  |  2KB  |  46 lines

  1.  
  2. /*  File   : strnrev.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 1 June 1984
  5.     Defines: strnrev()
  6.  
  7.     strnrev(dst, src, len)
  8.     copies all the characters of src to dst, in REVERSE order.  If src
  9.     was terminated by a NUL character, so will dst be, otherwise dst &
  10.     src are both exactly len non-NUL characters long.  This returns no
  11.     result.  It is to strrev as strncpy is to strcpy.
  12.  
  13.     Note: this function is perfectly happy to reverse a string into the
  14.     same place, strnrev(x, x, L) will work.
  15.     It will not work for partially overlapping source and destination.
  16. */
  17.  
  18. #include "strings.h"
  19.  
  20. void strnrev(dsta, srca, len)
  21.     register char *dsta, *srca;
  22.     register int len;
  23.     {
  24.         register char *dstz, *srcz;
  25.         register int t;
  26.         /*  On a machine which doesn't supply 6 register variables,
  27.             you could #define t len, as the two variables don't overlap.
  28.         */
  29.  
  30.         for (srcz = srca; --len >= 0 && *srcz; srcz++) ;
  31.         dstz = dsta+(srcz-srca);
  32.         /*  If srcz was stopped by len running out, it points just after
  33.             the last character of the source string, and it and dstz are
  34.             just right.  Otherwise, it was stopped by hitting NUL, and is
  35.             in the right place, but dstz should get a NUL as well.
  36.         */
  37.         if (len >= 0) *dstz = NUL;
  38.         /*  That was the very last use of len  */
  39.         while (srcz > srca) {
  40.             t = *--srcz;
  41.             *--dstz = *srca++;
  42.             *dsta++ = t;
  43.         }
  44.     }
  45.  
  46.